package titiritero.vista;

import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.IOException;
import javax.imageio.ImageIO;
import java.io.FileInputStream;
import titiritero.Dibujable;
import titiritero.Posicionable;
import titiritero.SuperficieDeDibujo;

public class Imagen extends Figura implements Dibujable {

    private BufferedImage imagen;
    private Posicionable posicionable;
    protected int alto;
    protected int ancho;
    protected int type;
    
    public Imagen(String rutaDelArchivo) {
    	try {
    		
    		FileInputStream fis = new FileInputStream(rutaDelArchivo);
			BufferedImage originalImage = ImageIO.read(fis);
			
			imagen = originalImage;
		} catch (IOException e) {
			e.printStackTrace();
			System.exit(1);
		}
    }
   
    public void setDimension(int ancho, int alto){
		this.alto = alto;
		this.ancho = ancho;
    	BufferedImage originalImage = null;
		originalImage = this.imagen;
		
		type = originalImage.getType() == 0? BufferedImage.TYPE_INT_ARGB : originalImage.getType();
		this.setImagen(imagen);
    }
    
    protected BufferedImage resizeImage(BufferedImage originalImage, int type){
	BufferedImage resizedImage = new BufferedImage(ancho, alto, type);
	Graphics2D g = resizedImage.createGraphics();
	g.drawImage(originalImage, this.getPosicionable().getX(), this.getPosicionable().getY(), ancho, alto, null);
	g.dispose();
 
	return resizedImage;
    }
    
	public void dibujar(SuperficieDeDibujo superficieDeDibujo) {
		
		BufferedImage resizedImage = new BufferedImage(ancho, alto, type);
		Graphics2D g = resizedImage.createGraphics();
		g.drawImage(this.imagen,getPosicionable().getX() - (getAlto()/2) , getPosicionable().getY() - (getAncho()/2),null);
	}
	
	public Posicionable getPosicionable() {
		return posicionable;
	}
	
	public void setPosicionable(Posicionable unPosicionable){
		
		this.posicionable = unPosicionable;
	}
	
	public int getAncho(){
		return this.ancho;
	}
	
	public int getAlto(){
		return this.alto;
	}

	public BufferedImage getImagen() {
		return this.imagen;
	}

	public void setImagen(BufferedImage imagen) {
		this.imagen = imagen;
	}
	
}
